<template>
  <simple-table
    :url-list="urlList"
    :current-use-btn-list="currentUseBtnList"
    :filed-property-list="filedPropertyList"
    width="75%"
    submit-form-title="当日生产计划"
    @reset-submit-form="resetSubmitForm"
    @reset-submit-data="resetSubmitData"
  >
  </simple-table>
</template>

<script lang="ts" setup>
import { TableUrl } from "@/models/TableSetting";
import SimpleTable from "@/components/table/simple/SimpleTable.vue";
import { BtnNameEnum } from "@/components/table/simple/BtnNameEnum";
import { RuleEnum } from "@/components/form/basic/FormEnum";
import { reactive, ref, Ref } from "vue";
import api from "@/utils";
import { AxiosResponse } from "axios";
import { ElMessage } from "element-plus/es";
import FiledProperty from "@/models/FiledProperty";

const urlList = {
  refreshUrl:
    "/receipts/daily_production_plan_receipt/search/" +
    sessionStorage.getItem("queryCode"),
  modifyingSingleUrl:
    "/receipts/daily_production_plan_receipt/" +
    sessionStorage.getItem("authorityCode"),
  deleteUrl: "/receipts/daily_production_plan_receipt/batch-delete",
  invalidUrl: "/receipts/daily_production_plan_receipt/batchInvalid",
} as TableUrl;

// 当前页面使用的功能按钮
const currentUseBtnList: BtnNameEnum[] = [
  BtnNameEnum.ADD,
  BtnNameEnum.REFRESH,
  BtnNameEnum.DELETE,
  BtnNameEnum.BATCH_IMPORT,
  BtnNameEnum.BATCH_INVALID,
  BtnNameEnum.ADVANCED_SEARCH,
];

const filedPropertyList = reactive([
  {
    filedName: "id",
    label: "ID",
    span: 8,
    width: 100,
    minWidth: 100,
    showInTable: true,
    showInForm: false,
    formType: "el-form-text",
  },
  {
    filedName: "invalid",
    label: "状态",
    span: 8,
    width: 220,
    minWidth: 120,
    showInTable: true,
    showInForm: false,
    showInSearch: true,
    formType: "el-form-select-dropdown",
    meta: {
      optionList: [
        { label: "作废", value: "作废" },
        { label: "正常", value: "正常" },
        { label: "全部", value: "" },
      ],
    },
  },
  {
    filedName: "no",
    label: "内部编码",
    span: 8,
    width: 220,
    minWidth: 120,
    showInTable: true,
    showInForm: true,
    showInSearch: true,
    formType: "el-form-text",
    meta: {
      disabled: true,
    },
  },
  {
    filedName: "noHand",
    label: "手写编码",
    span: 8,
    width: 220,
    minWidth: 120,
    showInTable: true,
    showInForm: true,
    showInSearch: true,
    formType: "el-form-text",
  },
  {
    filedName: "name",
    label: "名称",
    span: 8,
    width: 220,
    minWidth: 120,
    showInTable: true,
    showInForm: true,
    showInSearch: true,
    formType: "el-form-text",
    rules: [
      {
        ruleName: RuleEnum.REQUIRED,
        ruleValue: true,
        message: "名称不能为空",
      },
    ],
  },
  {
    filedName: "idJson",
    label: "json数据",
    span: 8,
    minWidth: 120,
    showInTable: true,
    showInForm: true,
    jsonFiled: [
      {
        filedName: "plannedDate",
        label: "计划日期",
        span: 8,
        width: 230,
        minWidth: 120,
        showInTable: true,
        showInForm: true,
        required: true,
        formType: "el-form-picker-date",
      },
      {
        filedName: "editionNo",
        label: "版本号",
        span: 8,
        width: 230,
        minWidth: 130,
        showInTable: true,
        showInForm: true,
        formType: "el-form-text",
      },
      {
        filedName: "planNumber",
        label: "计划总环数",
        span: 8,
        width: 230,
        minWidth: 130,
        showInTable: true,
        showInForm: true,
        formType: "el-form-text",
      },
      {
        filedName: "completeNumber",
        label: "累计完成环数",
        span: 8,
        width: 130,
        minWidth: 130,
        showInTable: true,
        showInForm: true,
        formType: "el-form-text",
      },
      {
        filedName: "preparationPerson",
        label: "编制人",
        span: 8,
        width: 230,
        minWidth: 130,
        showInTable: true,
        showInForm: true,
        formType: "el-form-text",
      },
      {
        filedName: "preparationDate",
        label: "编制时间",
        span: 8,
        width: 130,
        minWidth: 130,
        showInTable: true,
        showInForm: true,
        formType: "el-form-picker-date",
      },
      {
        filedName: "modifyPerson",
        label: "修改人",
        span: 8,
        width: 130,
        minWidth: 130,
        showInTable: true,
        showInForm: true,
        formType: "el-form-text",
      },
      {
        filedName: "modifyDate",
        label: "修改时间",
        span: 8,
        width: 130,
        minWidth: 130,
        showInTable: true,
        showInForm: true,
        formType: "el-form-picker-date",
      },
      {
        filedName: "reviewPerson",
        label: "审核人",
        span: 8,
        width: 130,
        minWidth: 130,
        showInTable: true,
        showInForm: true,
        formType: "el-form-text",
      },
      {
        filedName: "reviewDate",
        label: "审核时间",
        span: 8,
        width: 130,
        minWidth: 130,
        showInTable: true,
        showInForm: true,
        formType: "el-form-picker-date",
      },
    ],
  },
  {
    filedName: "remark",
    label: "备注",
    span: 24,
    showInTable: true,
    showInForm: true,
    showInSearch: true,
    formType: "el-form-area-text",
  },
  {
    filedName: "receiptsSub",
    label: "详情",
    span: 8,
    width: 120,
    minWidth: 120,
    showInTable: false,
    showInForm: true,
    children: [
      {
        filedName: "id",
        label: "ID",
        span: 8,
        width: 120,
        minWidth: 120,
        showInTable: false,
        showInForm: false,
        formType: "el-form-text",
      },
      {
        filedName: "no",
        label: "主表编号",
        span: 8,
        width: 120,
        minWidth: 120,
        showInTable: false,
        showInForm: false,
        formType: "el-form-text",
      },
      {
        filedName: "subNo",
        label: "编号",
        span: 8,
        width: 180,
        minWidth: 120,
        showInTable: true, // 该字段内容是否需要在表格中渲染显示.
        showInForm: true,
        required: true,
        formType: "el-form-text",
      },
      {
        filedName: "subName",
        label: "名称",
        span: 8,
        width: 180,
        minWidth: 120,
        showInTable: true, // 该字段内容是否需要在表格中渲染显示.
        showInForm: true,
        required: true,
        formType: "el-form-text",
      },
      {
        filedName: "subJson",
        label: "子json数据",
        span: 8,
        width: 180,
        minWidth: 120,
        showInTable: true,
        showInForm: true,
        jsonFiled: [
          {
            filedName: "homemadeNo",
            label: "自制件编码",
            span: 8,
            minWidth: 120,
            showInTable: true,
            showInForm: true,
            formType: "el-form-text",
          },
          {
            filedName: "model",
            label: "型号",
            span: 8,
            minWidth: 120,
            showInTable: true,
            showInForm: true,
            formType: "el-form-text",
          },
          {
            filedName: "technicalRequirement",
            label: "技术要求",
            span: 8,
            minWidth: 120,
            showInTable: true,
            showInForm: true,
            formType: "el-form-text",
          },
          {
            filedName: "unit",
            label: "单位",
            span: 8,
            minWidth: 120,
            showInTable: true,
            showInForm: true,
            formType: "el-form-text",
          },
          {
            filedName: "quantity",
            label: "数量",
            span: 8,
            minWidth: 120,
            showInTable: true,
            showInForm: true,
            formType: "el-form-text",
          },
        ],
      },
      {
        filedName: "remark",
        label: "备注",
        span: 8,
        width: 180,
        minWidth: 120,
        showInTable: true,
        showInForm: true,
        required: true,
        formType: "el-form-text",
      },
    ],
  },
  {
    filedName: "createUser",
    label: "创建人",
    span: 8,
    minWidth: 220,
    showInTable: true,
    showInForm: false,
    formType: "el-form-text",
  },
  {
    filedName: "createGmt",
    label: "创建时间",
    span: 8,
    minWidth: 230,
    showInTable: true,
    showInForm: false,
    formType: "el-form-picker-date",
  },
  {
    filedName: "modifiedUser",
    label: "修改人",
    span: 8,
    minWidth: 220,
    showInTable: true,
    showInForm: false,
    formType: "el-form-text",
  },
  {
    filedName: "modifiedGmt",
    label: "修改时间",
    span: 8,
    minWidth: 230,
    showInTable: true,
    showInForm: false,
    formType: "el-form-picker-date",
  },
]) as Array<FiledProperty>;
const receiptsNo = ref();
// 获取默认属性值
const getReceiptNo = async () => {
  const date = new Date();
  const dateFormat = api.formatDate(date, "yyyyMM");
  const userNo = sessionStorage.getItem("userNo") as string;
  await api.http
    .get(
      "/receipts/daily_production_plan_receipt" +
        "/generateReceiptsNo/012" +
        userNo +
        dateFormat
    )
    .then((res: AxiosResponse) => {
      receiptsNo.value = res.data.data;
    })
    .catch((err) => {
      ElMessage.error(err);
      console.log(err);
    });
};
// 给默认属性赋值
const resetSubmitForm = (
  submitForm: Ref,
  callback: (callBackSubmitForm: Ref) => void
) => {
  getReceiptNo().then(() => {
    submitForm.value.no = receiptsNo.value;
    callback(submitForm);
  });
};
const resetSubmitData = (
  submitData: Ref,
  callback: (callBackSubmitForm: Record<string, unknown>) => void
) => {
  submitData["receiptsSub"].forEach((item: Record<string, unknown>) => {
    item.no = submitData["no"];
  });
  callback(submitData.value);
};
</script>

<style scoped></style>
